package com.jp.kefu.web.controller;

import com.jp.kefu.service.MessageService;
import com.jp.kefu.util.ServerValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 处理来自微信公众平台的请求
 * @author lijin
 */
@RestController
@RequestMapping("/callback")
public class CallbackController {
    private Logger log = LoggerFactory.getLogger(getClass());
    @Autowired
    private MessageService messageService;

    //映射微信服务器验证请求
    @RequestMapping(params = {"echostr"})
    public String validate(String signature, String timestamp, String nonce, String echostr){
	   return ServerValidator.validate(signature, timestamp, nonce, echostr);
    }

    /**
     *
     * @param body
     * @return
     */
    @RequestMapping
    public String message(HttpServletRequest request, @RequestBody String body){
        log.info(body);
        return messageService.receive(body);

    }

}
